<template>
    <div>
        <div id="box">
            <span id="pet" class="fa fa-heart-o"></span>
            <router-link :to="'/user/namelist/'+this.name"><span id="search" class="fa fa-search" @touchstart="find"></span></router-link>
            <input type="text" v-model="name" @input="wordChange">
            <span class="fa fa-user-circle" id="user" @touchstart="btnClick"></span>
        </div>
          <ul id="search-box" v-show="searchResult.length>0">
            <li v-for="g in searchResult" class="item" @touchstart="search(g)">
                {{g.name}}
            </li>
        </ul>

    </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex';
export default {
    data: function() {
        return {
            name: "",
             timer:null,
            searchResult:[],
        }
    },
    methods: {
        search:function(g){
             var param = {
            name: g.name
        }
        this.$store.dispatch("requesthome", param);
        location.hash="/user/namelist/:qwe"
        },
        btnClick: function() {
            location.hash = "/user/login"
        },
        find: function() {
           
        },
         wordChange:function(){
            if(this.timer){
                clearTimeout(this.timer);
                this.timer = null;
            }

            this.timer = setTimeout(function() {
                // console.log("发送请求");
                if(this.name){
                    // console.log(123)
                    this.http.post("/api/key",{
                        params:{word:this.name}
                    })
                    .then(res=>{
                        console.log(res.data.data);
                        this.searchResult = res.data.data;
                    })
                }else{
                    this.searchResult = [];
                }
                this.timer = null;
            }.bind(this), 700);
        }
    
    },
    mounted: function() {


    }
}
</script>

<style scoped>
#search-box{
        border: solid 1px black;
        width: 60vw;
        position: absolute;
        left: 20vw;
        top: 6vh;
        margin: 0;
        z-index: 500;
        background-color: #EFEFEF;
        cursor: pointer;
        border-radius: 5px;
    }
a{
    text-decoration: none;
    color: black;
}
#box {
    width: 100%;
    height: 7vh;
    background-color: #FC4A00;
    position: fixed;
    top:0;
    left: 0;
    z-index: 200;
}

input {
    margin-left: 20vw;
    margin-top: 1vh;
    background-color: #FFFFFF;
    width: 60vw;
    height: 5vh;
    border: solid 1px #FC4A00;
    border-radius: 8px;
}

#pet {
    position: absolute;
    font-size: 30px;
    color: white;
    top: 1.2vh;
    left: 4vw;
}

#user {
    position: absolute;
    font-size: 30px;
    color: white;
    top: 1.2vh;
    right: 4vw;
}

#search {
    position: absolute;
    font-size: 25px;
    color: lightgray;
    top: 1.4vh;
    right: 21vw;
}

span {
    cursor: pointer;
}
</style>